using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using IPMS.Common;
using Resources;

public partial class OptCountries : Page, IRequiresSessionState
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }
    
    private void GetCountryDeleteList(string country, string engName, string chnName)
    {
        CountriesDB objDB = new CountriesDB();
        gvDeleted.DataSource = objDB.getCountriesList(country, engName, chnName, 1);
        gvDeleted.DataBind();
        objDB.CloseDB();
    }
    
    private void GetCountryActiveList(string country, string engName, string chnName)
    {
        CountriesDB objDB = new CountriesDB();
        gvActive.DataSource = objDB.getCountriesList(country, engName, chnName, 0);
        gvActive.DataBind();
        objDB.CloseDB();
    }
    
    private void GetCountryList(string country, string engName, string chnName)
    {
        if (SysUtil.IsAdministratorUsertype())
        {
            GetCountryDeleteList(country, engName, chnName);
        }

        GetCountryActiveList(country, engName, chnName);
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!Page.IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.MaintenanceFiles + " | " + ISitemap.CountryList.ToString();
            
            if (!SysUtil.IsAdministratorUsertype())
            {
                panelDeleted.Visible = false;
            }

            GetCountryList("", "", "");
        }
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetCountryList(txtSearchCountry.Text.Trim(), txtSearchEnglishName.Text.Trim(), txtSearchChineseName.Text.Trim());
    }
    
    protected void btnNewCountry_Click(object sender, EventArgs e)
    {
        Response.Redirect("OptCountriesAdd.aspx");
    }
    
    protected void gvActive_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvActive.PageIndex = e.NewPageIndex;
        GetCountryActiveList(txtSearchCountry.Text.Trim(), txtSearchEnglishName.Text.Trim(), txtSearchChineseName.Text.Trim());
    }


    protected void gvDeleted_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDeleted.PageIndex = e.NewPageIndex;
        GetCountryDeleteList(txtSearchCountry.Text.Trim(), txtSearchEnglishName.Text.Trim(), txtSearchChineseName.Text.Trim());
    }

    protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandSource is ImageButton)
        {
            CountriesDB objDB = new CountriesDB();
            GridView gv = (GridView)sender;
            GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            string strCountry = gv.DataKeys[curRow.RowIndex].Values["Country"].ToString();

            if (e.CommandName == "halfDelete")
            {
                if (objDB.Delete(strCountry,
                                    Page.User.Identity.Name.ToString(),
                                    HttpContext.Current.Request.UserHostAddress) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_country, "Delete Country Master Success! Country Code: " + strCountry);
                    Jscript.Alert("Delete Country Master Success!", Page);
                    GetCountryList(txtSearchCountry.Text.Trim(), txtSearchEnglishName.Text.Trim(), txtSearchChineseName.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_country, "Delete Country Master Failure! Country Code: " + strCountry);
                    Jscript.Alert("Delete Country Master Failure!", Page);
                }
            }
            else if (e.CommandName == "Restore")
            {
                // do restore
                if (objDB.Restore(strCountry) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_country, "Restore Country Master Success! Country Code: " + strCountry);
                    Jscript.Alert("Restore Country Master Success!", Page);
                    GetCountryList(txtSearchCountry.Text.Trim(), txtSearchEnglishName.Text.Trim(), txtSearchChineseName.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_country, "Restore Country Master Failure! Country Code: " + strCountry);
                    Jscript.Alert("Restore Country Master Failure!", Page);
                }
            }
            else if (e.CommandName == "ForceDelete")
            {
                if (objDB.Trash(strCountry) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_country, "Trash Country Master Success! Country Code: " + strCountry);
                    Jscript.Alert("Trash Country Master Success!", Page);
                    GetCountryList(txtSearchCountry.Text.Trim(), txtSearchEnglishName.Text.Trim(), txtSearchChineseName.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_country, "Trash Country Master Failure! Country Code: " + strCountry);
                    Jscript.Alert("Trash Country Master Failure!", Page);
                }
            }

            objDB.CloseDB();
        }
    }
}
